<template>
  <div style="padding: 20px;">
    <h2>字符选择器演示</h2>
    
    <!-- 测试输入框 -->
    <div style="margin-bottom: 20px;">
      <h3>测试输入框</h3>
      <el-input
        v-model="testText1"
        placeholder="在这里测试光标插入功能"
        ref="testInput1Ref"
        @focus="onInputFocus($event.target)"
        @click="onInputFocus($event.target)"
        @keyup="updateCursor"
      />
      
      <el-input
        v-model="testText2"
        placeholder="在这里也可以测试"
        ref="testInput2Ref"
        style="margin-top: 10px;"
        @focus="onInputFocus($event.target)"
        @click="onInputFocus($event.target)"
        @keyup="updateCursor"
      />
    </div>
    
    <!-- 对话框版本 -->
    <div style="margin-bottom: 30px;">
      <h3>使用对话框</h3>
      <el-button type="primary" @click="openDialog">打开字符选择器(对话框)</el-button>
      
      <el-dialog
        v-model="dialogVisible"
        title="字符选择器"
        width="600px"
      >
        <CharacterSelector 
          ref="charSelectorRef"
          @character-selected="onCharacterSelected"
        />
        
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="closeDialog">取消</el-button>
            <el-button type="primary" @click="confirmSelection">确定</el-button>
          </span>
        </template>
      </el-dialog>
    </div>
    
    <!-- 抽屉版本 -->
    <div>
      <h3>使用抽屉</h3>
      <el-button type="success" @click="openDrawer">打开字符选择器(抽屉)</el-button>
      
      <el-drawer
        v-model="drawerVisible"
        title="字符选择器"
        direction="rtl"
        size="600px"
      >
        <CharacterSelector 
          ref="drawerCharSelectorRef"
          @character-selected="onCharacterSelected"
        />
        
        <template #footer>
          <div style="display: flex; justify-content: space-between;">
            <el-button @click="closeDrawer">关闭</el-button>
            <el-button type="primary" @click="useSelectedChars">使用选中字符</el-button>
          </div>
        </template>
      </el-drawer>
    </div>
    
    <!-- 显示结果 -->
    <div style="margin-top: 30px;">
      <h3>选中的字符</h3>
      <el-input
        v-model="resultText"
        placeholder="选中的字符将显示在这里"
        ref="resultInputRef"
        @focus="onInputFocus($event.target)"
        @click="onInputFocus($event.target)"
        @keyup="updateCursor"
      >
        <template #append>
          <el-button @click="copyResult">复制</el-button>
        </template>
      </el-input>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import CharacterSelector from './components/CharacterSelector.vue'
import { useCursorTracker } from './composables/useCursorTracker'

// 使用光标跟踪器
const { setActiveInput, updateCursorPosition, insertAtCursor } = useCursorTracker()

// 测试输入框
const testText1 = ref('')
const testText2 = ref('')
const testInput1Ref = ref(null)
const testInput2Ref = ref(null)

// 对话框相关
const dialogVisible = ref(false)
const charSelectorRef = ref(null)

// 抽屉相关
const drawerVisible = ref(false)
const drawerCharSelectorRef = ref(null)

// 结果
const resultText = ref('')
const resultInputRef = ref(null)

// 输入框聚焦事件
const onInputFocus = (inputElement) => {
  setActiveInput(inputElement)
}

// 更新光标位置
const updateCursor = () => {
  updateCursorPosition()
}

// 对话框方法
const openDialog = () => {
  dialogVisible.value = true
  if (charSelectorRef.value) {
    charSelectorRef.value.clearSelection()
  }
}

const closeDialog = () => {
  dialogVisible.value = false
}

const confirmSelection = () => {
  if (charSelectorRef.value) {
    const selectedChars = charSelectorRef.value.getAndClearSelectedChars()
    insertAtCursor(selectedChars)
  }
  dialogVisible.value = false
}

// 抽屉方法
const openDrawer = () => {
  drawerVisible.value = true
  if (drawerCharSelectorRef.value) {
    drawerCharSelectorRef.value.clearSelection()
  }
}

const closeDrawer = () => {
  drawerVisible.value = false
}

const useSelectedChars = () => {
  if (drawerCharSelectorRef.value) {
    const selectedChars = drawerCharSelectorRef.value.getAndClearSelectedChars()
    insertAtCursor(selectedChars)
  }
  drawerVisible.value = false
}

// 处理字符选择事件
const onCharacterSelected = (char) => {
  console.log('选择了字符:', char)
  // 直接在当前光标位置插入字符
  //insertAtCursor(char)
}

const copyResult = async () => {
  if (!resultText.value) {
    ElMessage.warning('没有内容可复制')
    return
  }
  
  try {
    await navigator.clipboard.writeText(resultText.value)
    ElMessage.success('已复制到剪贴板')
  } catch (err) {
    ElMessage.error('复制失败')
  }
}
</script>